//
//  TMGrid.m
//  cutter
//
//  Created by mac on 15/12/11.
//  Copyright © 2015年 e360. All rights reserved.
//

#import "TMGrid.h"

@implementation TMGrid
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        [self setUserInteractionEnabled:NO];
        [self setRefine:5];
        [self setUnit:100];
        [self setScale:1];
    }
    return self;
}
-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    if (self.unit > 0 && self.scale > 0 && self.refine > 0) {
        CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
        //显示范围
        CGFloat len = [self power:1.0 / self.scale] * self.unit * self.scale;
        CGFloat top = [self offset:self.offsetY + center.y refine:len] - center.y;
        CGFloat lef = [self offset:self.offsetX + center.x refine:len] - center.x;
        CGFloat bot = rect.size.height - top;
        CGFloat rig = rect.size.width - lef;
        CGFloat ref = len / self.refine;
        //
        CGMutablePathRef rPath = CGPathCreateMutable();
        CGMutablePathRef uPath = CGPathCreateMutable();
        //横行
        uint rows = 0;
        CGFloat offy = top;
        while (offy < bot) {
            if (rows % self.refine == 0){
                CGPathMoveToPoint(uPath, NULL, lef, offy);
                CGPathAddLineToPoint(uPath, NULL, rig, offy);
            }else{
                CGPathMoveToPoint(rPath, NULL, lef, offy);
                CGPathAddLineToPoint(rPath, NULL, rig, offy);
            }
            offy += ref;
            rows++;
        }
        //竖线
        uint cols = 0;
        CGFloat offx = lef;
        while (offx < rig) {
            if (cols % self.refine == 0){
                CGPathMoveToPoint(uPath, NULL, offx, top);
                CGPathAddLineToPoint(uPath, NULL, offx, bot);
            }else{
                CGPathMoveToPoint(rPath, NULL, offx, top);
                CGPathAddLineToPoint(rPath, NULL, offx, bot);
            }
            offx += ref;
            cols++;
        }
        //画路径
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, center.x, center.y);
        CGContextRotateCTM(context, 0);
        CGContextSetLineWidth(context, 1);
        CGContextSetShouldAntialias(context,NO);
        CGContextSetStrokeColorWithColor(context, [[UIColor darkGrayColor] CGColor]);
        CGContextAddPath(context, uPath);
        CGContextDrawPath(context, kCGPathStroke);
        CGContextSetStrokeColorWithColor(context, [[UIColor lightGrayColor] CGColor]);
        CGContextAddPath(context, rPath);
        CGContextDrawPath(context, kCGPathStroke);
        CGPathRelease(rPath);
        CGPathRelease(uPath);
    }
}
-(CGFloat)power:(CGFloat)v{
    if (v != 0) {
        uint mul = 1;
        while (v * mul < 1) {
            mul <<= 1;
        }
        //
        uint tmp = round(v * mul);
        while (tmp != 0) {
            v = tmp;
            tmp = tmp & (tmp - 1);
        }
        return v / mul;
    }
    return 0;
}
-(CGFloat)offset:(CGFloat)value refine:(CGFloat)refine{
    if (refine != 0) {
        return value - ceil(value / refine) * refine;
    }
    return 0;
}
@end